package com.ezhuan.users.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.service.BaseServiceImpl;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.pojo.ARegion;
import com.ezhuan.system.service.ISystemService;
import com.ezhuan.users.dao.IAUsersDao;
import com.ezhuan.users.dao.IAUsersWithdrawDao;
import com.ezhuan.users.dao.IAWithdrawHistoryDao;
import com.ezhuan.users.dao.impl.AUsersDaoImpl;
import com.ezhuan.users.dao.impl.AUsersWithdrawDaoImpl;
import com.ezhuan.users.dao.impl.AWithdrawHistoryDaoImpl;
import com.ezhuan.users.pojo.AUsers;
import com.ezhuan.users.pojo.AUsersWithdraw;
import com.ezhuan.users.pojo.AWithdrawHistory;
import com.ezhuan.users.service.IUsersService;

/**
 * 用户相关的Service实现类
 * 
 * @author wangshSxt
 *
 */
public class UsersServiceImpl extends BaseServiceImpl implements IUsersService
{
	private IAUsersDao usersDao = new AUsersDaoImpl();
	private IAUsersWithdrawDao usersWithdrawDao = new AUsersWithdrawDaoImpl() ; 
	private IAWithdrawHistoryDao withdrawHistoryDao = new AWithdrawHistoryDaoImpl() ;
	private ISystemService systemService = (ISystemService) ConstatFinalUtil.BEANMAP.get("systemService");

	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap)
	{
		AUsers users = this.usersDao.findOneDao(condMap);
		if(users != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			/* 查询关联对象 */
			/* 查询地区 */
			extMap.clear();
			extMap.put("id", users.getRegionId());
			ARegion region = this.systemService.findOneRegionService(extMap);
			if(region != null)
			{
				/* 为treeName赋值 */
				this.systemService.findTreeRegionService(region); 
			}
			users.setRegionObject(region);
			
			/* 查询手机归属地地区 */
			extMap.clear();
			extMap.put("id", users.getPhoneRegionId());
			ARegion regionPhone = this.systemService.findOneRegionService(extMap);
			if(regionPhone != null)
			{
				/* 为treeName赋值 */
				this.systemService.findTreeRegionService(regionPhone); 
			}
			users.setPhoneRegionObject(regionPhone);
			
			/* 查询ip地区 */
			extMap.clear();
			extMap.put("id", users.getIpRegionId());
			ARegion regionIp = this.systemService.findOneRegionService(extMap);
			if(regionIp != null)
			{
				/* 为treeName赋值 */
				this.systemService.findTreeRegionService(regionIp); 
			}
			users.setIpRegionObject(regionIp);
		}
		return users ; 
	}

	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AUsers> usersList = this.usersDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = usersList.iterator(); iterator.hasNext();)
			{
				AUsers users = (AUsers) iterator.next();
				
				/* 查询关联对象 */
				/* 查询地区 */
				extMap.clear();
				extMap.put("id", users.getRegionId());
				ARegion region = this.systemService.findOneRegionService(extMap);
				if(region != null)
				{
					/* 为treeName赋值 */
					this.systemService.findTreeRegionService(region); 
				}
				users.setRegionObject(region);
				

				/* 查询手机归属地地区 */
				extMap.clear();
				extMap.put("id", users.getPhoneRegionId());
				ARegion regionPhone = this.systemService.findOneRegionService(extMap);
				if(regionPhone != null)
				{
					/* 为treeName赋值 */
					this.systemService.findTreeRegionService(regionPhone); 
				}
				users.setPhoneRegionObject(regionPhone);
				
				/* 查询ip地区 */
				extMap.clear();
				extMap.put("id", users.getIpRegionId());
				ARegion regionIp = this.systemService.findOneRegionService(extMap);
				if(regionIp != null)
				{
					/* 为treeName赋值 */
					this.systemService.findTreeRegionService(regionIp); 
				}
				users.setIpRegionObject(regionIp);
				
				/* 查询上一级对象 */
				/* 查询地区 */
				extMap.clear();
				extMap.put("id", users.getParentId());
				AUsers parentUsers = this.findOneUsersService(extMap);
				users.setParentUsersObject(parentUsers);
				
				/* 查询上两级对象 */
				/* 查询地区 */
				extMap.clear();
				extMap.put("id", users.getSecParentId());
				AUsers secParentUsers = this.findOneUsersService(extMap);
				users.setSecParentUsersObject(secParentUsers);
			}
		}
		return usersList ; 
	}

	@Override
	public JSONObject saveOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 手机号不能重复 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("phone", users.getPhone());
		AUsers usersRes = this.findOneUsersService(condMap);
		if(usersRes != null)
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
			return resultJSON ; 
		}
		
		/* 生成当前用户的邀请码 */
		String invitationCode = "" ; 
		while(true)
		{
			/* 不停的生成新的邀请码 */
			invitationCode = this.regexUtil.randStr(8);
			/* 查询数据库邀请码是否存在 */
			condMap.clear();
			condMap.put("invitationCode", invitationCode);
			AUsers usersInviCode = this.findOneUsersService(condMap);
			if(usersInviCode == null)
			{
				/* 如果此邀请码不存在,就存储 */
				break ; 
			}
		}
		users.setInvitationCode(invitationCode);
		
		/* 邀请码处理,查看是否是别人邀请的新用户过来 */
		condMap.clear();
		condMap.put("invitationCode", users.getInvitationCodeStr());
		AUsers usersInviCode = this.findOneUsersService(condMap);
		if(usersInviCode != null)
		{
			/* 设置上一级Id */
			users.setParentId(usersInviCode.getId());
			users.setLevel(usersInviCode.getLevel() + 1);
			/* 设置二级节点;当前节点往上推 */
			users.setSecParentId(usersInviCode.getParentId());
		}
		
		/* 设置IP归属地 */
		String responseIP = this.ipLocalUtil.sendGet(users.getRegIp());
		JSONObject responseIPJSON = JSON.parseObject(responseIP);
		if("200".equalsIgnoreCase(responseIPJSON.getString("resultcode")))
		{
			JSONObject responseResIPJSON = responseIPJSON.getJSONObject("result");
			String area = responseResIPJSON.get("area") + "";
			String location = responseResIPJSON.get("location") + "";
			users.setIpLocation(area + ";" + location);
			
			/* 查询地区 */
			condMap.clear();
			condMap.put("name", area);
			List<ARegion> regionList = this.systemService.findCondListRegionService(new PageInfoUtil(), condMap);
			if(regionList.size() > 0 )
			{
				/* 设置地区 */
				users.setIpRegionId(regionList.get(0).getId());
			}
		}
		/* 设置手机归属地 */
		String responsePhone = this.telLocalUtil.getRequest1(users.getPhone());
		JSONObject responsePhoneJSON = JSON.parseObject(responsePhone);
		if("200".equalsIgnoreCase(responsePhoneJSON.getString("resultcode")))
		{
			JSONObject responseResIPJSON = responsePhoneJSON.getJSONObject("result");
			String area = responseResIPJSON.get("city") + "";
			String location = responseResIPJSON.get("company") + "";
			users.setPhoneLocation(area + ";" + location);
			
			/* 查询地区 */
			condMap.clear();
			condMap.put("name", area);
			List<ARegion> regionList = this.systemService.findCondListRegionService(new PageInfoUtil(), condMap);
			if(regionList.size() > 0 )
			{
				/* 设置地区 */
				users.setPhoneRegionId(regionList.get(0).getId());
			}
		}
		
		/* 保存记录 */
		boolean flag = this.usersDao.save(users);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 手机号不能重复 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("phone", users.getPhone());
		AUsers usersRes = this.findOneUsersService(condMap);
		if(usersRes != null && usersRes.getId() != users.getId())
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
			return resultJSON ; 
		}
		
		/* 保存记录 */
		boolean flag = this.usersDao.update(users);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	
	@Override
	public AUsersWithdraw findOneUsersWithdrawService(Map<String, Object> condMap)
	{
		AUsersWithdraw usersWithdraw = this.usersWithdrawDao.findOneDao(condMap);
		if(usersWithdraw != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			
			/* 为扩展字段赋值 */
			/* 为用户赋值 */
			extMap.put("id", usersWithdraw.getUsersId());
			AUsers users = this.findOneUsersService(condMap);
			usersWithdraw.setUsersObject(users);
		}
		return usersWithdraw ; 
	}

	@Override
	public List<AUsersWithdraw> findCondListUsersWithdrawService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AUsersWithdraw> usersWithdrawList = this.usersWithdrawDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = usersWithdrawList.iterator(); iterator.hasNext();)
			{
				AUsersWithdraw usersWithdraw = (AUsersWithdraw) iterator.next();
				
			}
		}
		return usersWithdrawList ; 
	}

	@Override
	public JSONObject saveOneUsersWithdrawService(AUsersWithdraw usersWithdraw)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 先根据wtype和thirdName和usersId查询 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("usersId", usersWithdraw.getUsersId());
		condMap.put("withType", usersWithdraw.getWithType());
		AUsersWithdraw usersWithdrawRes = this.findOneUsersWithdrawService(condMap);
		if(usersWithdrawRes != null)
		{
			usersWithdrawRes.setThirdName(usersWithdraw.getThirdName());
			usersWithdrawRes.setExtend1(usersWithdraw.getExtend1());
			usersWithdrawRes.setUpdateTime(new Date());
			usersWithdrawRes.setPubTime(new Date());
			return this.updateOneUsersWithdrawService(usersWithdrawRes);
		}
		
		/* 保存记录 */
		boolean flag = this.usersWithdrawDao.save(usersWithdraw);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersWithdraw.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersWithdrawService(AUsersWithdraw usersWithdraw)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.usersWithdrawDao.update(usersWithdraw);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersWithdraw.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public AWithdrawHistory findOneWithdrawHistoryService(Map<String, Object> condMap)
	{
		AWithdrawHistory withdrawHistory = this.withdrawHistoryDao.findOneDao(condMap);
		if(withdrawHistory != null && 
				"true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
		}
		return withdrawHistory ; 
	}

	@Override
	public List<AWithdrawHistory> findCondListWithdrawHistoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AWithdrawHistory> withdrawHistoryList = this.withdrawHistoryDao.findCondListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String, Object> extMap = new HashMap<String,Object>();
			for (Iterator iterator = withdrawHistoryList.iterator(); iterator.hasNext();)
			{
				AWithdrawHistory withdrawHistory = (AWithdrawHistory) iterator.next();
				
			}
		}
		return withdrawHistoryList ; 
	}

	@Override
	public JSONObject saveOneWithdrawHistoryService(AWithdrawHistory withdrawHistory)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 根据提交资料Id查询信息 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", withdrawHistory.getWithdrawId());
		/* 查询扩展字段 */
		condMap.put("extend", "true");
		AUsersWithdraw usersWithdraw = this.findOneUsersWithdrawService(condMap);
		if(usersWithdraw == null)
		{
			/* 提现资料不正确 */
			resultJSON.put("code", "12");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("12"));
			return resultJSON ; 
		}
		
		/* 设置静态的提现资料 */
		withdrawHistory.setName(usersWithdraw.getThirdName());
		withdrawHistory.setExtend1(usersWithdraw.getExtend1());
		
		/* 判断金额 */
		AUsers users = usersWithdraw.getUsersObject();
		if(users.getMoney() < withdrawHistory.getMoney())
		{
			/* 余额不足 */
			resultJSON.put("code", "13");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("13"));
			return resultJSON ; 
		}
		
		/* 保存记录 */
		boolean flag = this.withdrawHistoryDao.save(withdrawHistory);
		if(flag)
		{
			/* 减少金额 */
			users.setMoney(users.getMoney() - withdrawHistory.getMoney());
			this.updateOneUsersService(users);
			
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", withdrawHistory.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneWithdrawHistoryService(AWithdrawHistory withdrawHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存记录 */
		boolean flag = this.withdrawHistoryDao.update(withdrawHistory);
		if(flag)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", withdrawHistory.getId() + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
}
